﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;

namespace Game.EnemyScripts
{
    class ChasingEnemy:IEnemy
    {
        private readonly Level _level;
        private Vector2 _position;
        private readonly float _speed;
        private bool _isOnGround;

        private readonly Animation _animation;
        private AnimationPlayer _player;

        private int _width, _height;
        private SoundEffect _revSound;
        private SoundEffectInstance _revSoundInstance;

        const int MaxVolDist = 100;
        const int MinVolDist = 1000;
        
        public ChasingEnemy(Level level, Vector2 position, float speed, Texture2D texture)
        {
            _level = level;
            _position = position;
            _speed = speed;

            _animation = new Animation(texture, 0.1f, true);

            _revSound = _level.Content.Load<SoundEffect>("Sound Effect/Robot Rev");
            _revSoundInstance = _revSound.CreateInstance();
            _revSoundInstance.IsLooped = true;

            _width = texture.Height;
            _height = 32; // eww, hardcode

            _player = new AnimationPlayer();
            _player.PlayAnimation(_animation);
        }
        
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            // Stinky Animation and animation player
            var pos = _position;
            pos.Y += _height;
            pos.X += _width/2f; 
            _player.Draw(gameTime, spriteBatch, pos, SpriteEffects.None);
        }

        public void Update(GameTime gameTime)
        {
            float direction = 0;
            if (_level.Player.Position.X > _position.X) // player on the right
                direction = 1;
            else if (_level.Player.Position.X < _position.X)
                direction = -1;

            _position.X += _speed*direction*(float)gameTime.ElapsedGameTime.TotalSeconds;

            if (!_isOnGround)
                _position.Y += 0.5f;
            
            HandleCollision();
        }

        private void HandleCollision()
        {
            var bounds = GetBoundingRect();

            foreach (var platform in _level.Platforms)
            {
                Vector2 depth = bounds.GetIntersectionDepth(platform.BoundingRect);
                if (depth != Vector2.Zero)
                {
                    float absDepthX = Math.Abs(depth.X);
                    float absDepthY = Math.Abs(depth.Y);
                    
                    if (!_isOnGround)
                    {
                        if (absDepthY < absDepthX && absDepthY < 10)
                            _position.Y = platform.Position.Y - _height;
                        else
                            _position.X = _position.X + depth.X;

                        _isOnGround = true;
                    }
                    else
                    {
                        _position.X = _position.X + depth.X;
                    }
                }
            }
        }

        public Rectangle GetBoundingRect()
        {
            return new Rectangle((int) _position.X, (int) _position.Y, _width, _height);
        }

        ~ChasingEnemy()
        {
            if (!_revSoundInstance.IsDisposed)
                _revSoundInstance.Stop(true);
        }
    }
}
